﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebAPI.Models
{
	public class MemberRepository :IRepository<Member>
	{
		private List<Member>  mMembers = new List<Member>();

		public MemberRepository()
		{
			Add(new Member
			    	{
			    		Email = "Justin@email.com"
							, FirstName = "Justin"
							, LastName = "Kohnen"
							, Level = 1
							, AccountNumber = 42
			    	});

			Add(new Member
			{
				Email = "Brian@email.com"
				, FirstName = "Brian"
				, LastName = "Yavorsky"
				, Level = 2
				, AccountNumber = 10
			});
		}

		public IEnumerable<Member> GetAll()
		{
			return mMembers;
		}

		public Member Get(int id)
		{
			return mMembers.Find(m => m.Id == id);
		}

		public Member Add(Member item)
		{
			item.Id = mMembers.Count + 1;
			mMembers.Add(item);
			return item;
		}

		public void Remove(int id)
		{
			mMembers.RemoveAll(m => m.Id == id);
		}

		public bool Update(Member item)
		{
			int index = mMembers.FindIndex(m => m.Id == item.Id);
			if(index == 1)
			{
				return false;
			}

			mMembers.RemoveAt(index);
			mMembers.Add(item);
			return true;
		}
	}
}